home *** CD-ROM | disk | FTP | other *** search
- /* file zqsort.c -- by ^z, 870823-...
- * my quicksort to sort out the ptr array ... based, at least initially,
- * on the Lightspeed C library qsort routine, specialized to the task
- * at hand here ...
- */
-
- #include <stdio.h>
- #include <strings.h>
- #include <proto.h>
- #include "qndxr.2.h"
-
- /* sort elements "first" through "last" */
-
- void zqsort (first, last)
- register char **first, **last;
- {
- register char **i, **j, *tmp;
-
- while (last - first > 1)
- {
- i = first;
- j = last;
- for (;;)
- {
- while (++i < last && compare_ptrs (i, first) < 0)
- ;
- while (--j > first && compare_ptrs (j, first) > 0)
- ;
- if (i >= j)
- break;
- tmp = *i;
- *i = *j;
- *j = tmp;
- }
- tmp = *first;
- *first = *j;
- *j = tmp;
- if (j - first < last - (j + 1))
- {
- zqsort (first, j);
- first = j + 1;
- }
- else
- {
- zqsort (j + 1, last);
- last = j;
- }
- }
- }
-
-
- /* function to compare two index ptrs and give a result appropriate
- * for quicksort to use in alphabetizing them....
- *
- * Since the words pointed to have already been turned into all capital
- * letters and delimiters have been filtered out, simply doing zstrcmp()
- * for KEY_LENGTH letters works fine!
- *
- * Slight modification to make the quicksort stable: if two words tie,
- * then we want to compare their pointers to make the lesser one come
- * out first in the sort ...
- */
-
- int compare_ptrs (p1, p2)
- register char **p1, **p2;
- {
- register int diff;
-
- diff = zstrcmp (*p1, *p2);
- if (diff == 0)
- diff = ((*p1 - *p2) > 0) ? 1 : -1;
- return (diff);
- }
-
-
-
- /* my function to compare two strings and give a result as to who is
- * alphabetically earlier. Note that this is almost the same as strncmp()
- * with the fixed value of KEY_LENGTH as the maximum comparison distance,
- * except that I must be sure to mask the characters to make them positive
- * (since we want to be able to handle the non-ASCII funny letters in
- * the Apple character set properly/consistently). If the masking isn't
- * done, then inconsistent results can occur with those non-ASCII chars!
- */
-
- int zstrcmp (s1, s2)
- register char *s1, *s2;
- {
- register int n = KEY_LENGTH;
-
- for (; --n && ((*s1 & 0xFF) == (*s2 & 0xFF)); s1++, s2++)
- if (!*s1) break;
-
- return ((*s1 & 0xFF) - (*s2 & 0xFF));
- }
-
-